package com.qianqian.service.controller;

import com.alibaba.fastjson.JSONObject;
import com.qianqian.service.model.CapitalAccount;
import com.qianqian.service.service.CapitalAccountService;
import com.qianqian.service.service.wxcpapi.WxCpUserService;
import com.qianqian.service.service.wxcpapi.impl.WxCpAccountRequestServiceImpl;
import com.qianqian.service.service.wxcpapi.impl.WxCpXingYunZhuanPanRequestServiceImpl;
import com.qianqian.service.service.wxcpapi.impl.WxCpZhuanPanServiceRequestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/auth")
public class Auth {

    @Autowired
    CapitalAccountService capitalAccountService;
    /**
     * 企业微信用户进入应用认证并跳转
     * @param code
     * @return
     */
    @RequestMapping(value = "/xingyunzhuanpan", produces = {"application/json;charset=UTF-8"})
    public String xingyunzhuanpan(String code, String state,Model model){
        //幸运转盘应用环境
        WxCpUserService wxCpUserService  = new WxCpUserService(new WxCpXingYunZhuanPanRequestServiceImpl());
        JSONObject userInfo = wxCpUserService.getUserInfo(code);
        if(userInfo.getInteger("errcode")==0){
            String userId = userInfo.getString("UserId");
            CapitalAccount capitalAccount = capitalAccountService.selectByUserId(userId);
            if(capitalAccount!=null) {
                model.addAttribute("tokenData","{\"userId\":\""+userId+"\",\"path\":\"#/zhuanPan\"}");
                return "xingyunzhuanpan/index";
            }else{
                CapitalAccount newCapitalAccount = new CapitalAccount();
                newCapitalAccount.setUserId(userId);
                newCapitalAccount.setYue(500.0);
                newCapitalAccount.setEdu(9500.0);
                newCapitalAccount.setYingliYue(-500.0);
                newCapitalAccount.setIsActivity(1);
                newCapitalAccount.setDanCiEdu(500.0);
                newCapitalAccount.setJifenYue(0.0);
                capitalAccountService.insertSelective(newCapitalAccount);
                model.addAttribute("tokenData","{\"userId\":\""+userId+"\",\"path\":\"#/zhuanPan\"}");
                return "xingyunzhuanpan/index";
            }
        }
        return "auth-fail";
    }
    /**
     * 企业微信用户进入应用认证并跳转
     * @param code
     * @return
     */
    @RequestMapping(value = "/guzhi", produces = {"application/json;charset=UTF-8"})
    public String guzhi(String code, String state,Model model){
        return "guZhi";
    }
    /**
     * 企业微信用户进入应用认证并跳转
     * @param code
     * @return
     */
    @RequestMapping(value = "/btc", produces = {"application/json;charset=UTF-8"})
    public String btc(String code, String state,Model model){
        return "btc";
    }
    /**
     * 企业微信用户进入应用认证并跳转
     * @param code
     * @return
     */
    @RequestMapping(value = "/shouZe", produces = {"application/json;charset=UTF-8"})
    public String shouZe(String code, String state,Model model){
        return "shouZe";
    }
    @RequestMapping(value = "/account", produces = {"application/json;charset=UTF-8"})
    public String account(String code, String state,Model model){
        //幸运转盘应用环境
        WxCpUserService wxCpUserService  = new WxCpUserService(new WxCpAccountRequestServiceImpl());
        JSONObject userInfo = wxCpUserService.getUserInfo(code);
        if(userInfo.getInteger("errcode")==0){
            String userId = userInfo.getString("UserId");
            CapitalAccount capitalAccount = capitalAccountService.selectByUserId(userId);
            if(capitalAccount!=null) {
                model.addAttribute("tokenData","{\"userId\":\""+userId+"\",\"path\":\"#/account\"}");
                return "xingyunzhuanpan/index";
            }else{
                CapitalAccount newCapitalAccount = new CapitalAccount();
                newCapitalAccount.setUserId(userId);
                newCapitalAccount.setYue(500.0);
                newCapitalAccount.setEdu(9500.0);
                newCapitalAccount.setYingliYue(-500.0);
                newCapitalAccount.setIsActivity(1);
                newCapitalAccount.setDanCiEdu(500.0);
                newCapitalAccount.setJifenYue(0.0);
                capitalAccountService.insertSelective(newCapitalAccount);
                model.addAttribute("tokenData","{\"userId\":\""+userId+"\",\"path\":\"#/account\"}");
                return "xingyunzhuanpan/index";
            }
        }
        return "auth-fail";
    }
    @RequestMapping(value = "/xingyunzhuanpanservice", produces = {"application/json;charset=UTF-8"})
    public String xingyunzhuanpanservice(String code, String state,Model model){
        //幸运转盘应用环境
        WxCpUserService wxCpUserService  = new WxCpUserService(new WxCpZhuanPanServiceRequestServiceImpl());
        JSONObject userInfo = wxCpUserService.getUserInfo(code);
        if(userInfo.getInteger("errcode")==0){
            String userId = userInfo.getString("UserId");
            CapitalAccount capitalAccount = capitalAccountService.selectByUserId(userId);
            if(capitalAccount!=null) {
                model.addAttribute("tokenData","{\"userId\":\""+userId+"\",\"path\":\"#/zhuanPanService\"}");
                return "xingyunzhuanpan/index";
            }
        }
        return "auth-fail";
    }

    @RequestMapping(value = "/xingyunzhuanpanweb", produces = {"application/json;charset=UTF-8"})
    public String xingyunzhuanpanweb(Model model){
        model.addAttribute("tokenData","{\"userId\":\"xiecong@idealbpm.com\",\"path\":\"#/zhuanPan\"}");
        return "xingyunzhuanpan/index";
    }
}
